# -*- coding: UTF-8 -*-
from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from info import create_app,db
from flask import current_app
from info.models import User


#创建app
app = create_app("dev")

#创建脚本管理器对象
manager = Manager(app)
#让迁移和app和数据库建立联系
Migrate(app,db)
#将数据库迁移的脚本添加到manager
manager.add_command("mysql",MigrateCommand)

@manager.option('-u','-username',dest='username')
@manager.option('-p','-password',dest='password')
@manager.option('-m','-mobile',dest='mobile')
def createsuperuser(username,password,mobile):
    '''创建超级管理员用户的脚本函数'''
    if not all([username,password,mobile]):
        print('缺少必传参数')
    else:
        user = User()
        user.nick_name = username
        user.password = password
        user.mobile = mobile
        #当is_admin为True时，才是管理员用户
        user.is_admin = True

        try:
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)

if __name__ == '__main__':

    print(app.url_map)
    manager.run()

